package com.canbot.u05.activity.customvoice.adapter;

import android.app.ActionBar;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.activity.customvoice.bean.QuestionBean;

import java.util.HashMap;
import java.util.List;

/**
 * Created by liu.heng  on 2018/5/24.
 */

public class CustomDialogListAdapter extends RecyclerView.Adapter<CustomDialogListAdapter.ViewHolder> implements View.OnClickListener {

        private Context context;

        private List<QuestionBean> mList;

        private LayoutInflater mLayoutInflater;

        private Callback mCallback;

        private int checked = -1;

        private PopupWindow popupWindow;

        private HashMap<Integer, Boolean> hashMap = new HashMap<Integer, Boolean>() {
                @Override
                public Boolean get(@Nullable Object key) {
                        Boolean aBoolean = super.get(key);
                        return aBoolean == null ? false : aBoolean;
                }
        };

        public interface Callback {

                void clickEvent(View v, int type);
        }

        public CustomDialogListAdapter(Context context, List<QuestionBean> list, Callback callback) {
                this.context = context;
                this.mList = list;
                mLayoutInflater = LayoutInflater.from(context);
                this.mCallback = callback;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View inflate = mLayoutInflater.inflate(R.layout.custom_dialog_list_item, parent, false);
                return new ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
                holder.qustion.setText(mList.get(position).getQuestion());
                Log.e("------>", hashMap.get(position) + "");
                holder.btn_list.setTag(position);

                holder.choose_btn.setTag(position);

                holder.btn_list.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                showPopupWindow(view, position);
                        }
                });

                holder.choose_btn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                final CheckBox checkbox = (CheckBox) view;
                                if (checkbox.isChecked()) {
                                        hashMap.put(position, true);//每个position为唯一索引,进行存和取
                                        mCallback.clickEvent(view, 1);
                                } else {
                                        hashMap.put(position, false);//每个position为唯一索引,进行存和取
                                        mCallback.clickEvent(view, 2);
                                }
                        }
                });
                holder.choose_btn.setChecked(hashMap.get(position));      //设置checkbox的状态
        }

        @Override
        public int getItemCount() {
                return mList == null ? 0 : mList.size();
        }

        public void initData(int size,boolean isChooseAll) {
                if (size > 0) {
                        for (int i = 0; i < size; i++) {
                                hashMap.put(i, isChooseAll);
                        }
                }
        }

        public void notify(List<QuestionBean> mList) {
                this.mList = mList;
                initData(mList.size(),false);
                notifyDataSetChanged();
        }

        public void notifyChooseAll(boolean isChooseAll){
                initData(mList.size(),isChooseAll);
                notifyDataSetChanged();
        }


        public void setChecked(int checked) {//设定一个选中的标志位，在activity中传入值。
                this.checked = checked;
        }

        public int getChecked() {
                return checked;
        }

        //弹框的显示
        public void showPopupWindow(View view, int position) {

                // 一个自定义的布局，作为显示的内容
                final View contentView = LayoutInflater.from(context).inflate(R.layout.custom_dialog_list_item_menu, null);

                final Button button_voice = (Button) contentView.findViewById(R.id.voice_btn);
                final Button button_iamge = (Button) contentView.findViewById(R.id.image_btn);
                final Button button_action = (Button) contentView.findViewById(R.id.action_btn);

                button_voice.setOnClickListener(this);
                button_iamge.setOnClickListener(this);
                button_action.setOnClickListener(this);

                button_voice.setTag(position);
                button_iamge.setTag(position);
                button_action.setTag(position);

                popupWindow = new PopupWindow(contentView,
                        ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);

                popupWindow.setTouchInterceptor(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                Log.i("mengdd", "onTouch : ");
                                return false;
                                // 这里如果返回true的话，touch事件将被拦截
                                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

                        }
                });

                // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
                popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
                        R.drawable.menu_background));

                // 设置好参数之后再show
                final int[] location = new int[2];
                view.getLocationOnScreen(location);
                popupWindow.showAsDropDown(view, -(int) context.getResources().getDimension(R.dimen.x300), -(int) context.getResources().getDimension(R.dimen.x45));

        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {

                        case R.id.voice_btn:
                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (mCallback != null) {
                                                mCallback.clickEvent(view, 3);
                                        }
                                }
                                break;
                        case R.id.image_btn:
                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (mCallback != null) {
                                                mCallback.clickEvent(view, 4);
                                        }
                                }
                                break;
                        case R.id.action_btn:
                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (mCallback != null) {
                                                mCallback.clickEvent(view, 5);
                                        }
                                }
                                break;
                        default:
                                break;
                }
        }


        class ViewHolder extends RecyclerView.ViewHolder {

                private TextView qustion;

                private Button btn_list;

                private CheckBox choose_btn;

                public ViewHolder(View itemView) {
                        super(itemView);
                        qustion = (TextView) itemView.findViewById(R.id.qustion_text);
                        btn_list = (Button) itemView.findViewById(R.id.list_btn);
                        choose_btn = (CheckBox) itemView.findViewById(R.id.choose_btn);
                }
        }

}
